(module test
	(import latup)
	(export test-all)
	(main main))

(define (preprocess-string inp)
  (with-output-to-string 
    (lambda ()
      (with-input-from-string inp
	preprocess-current-io))))

(define print-test-results #f)

(define (test inp expected)
  (define result (preprocess-string inp))
  (if (equal? expected result)
      (when print-test-results
	    (print (format "(preprocess ~A) == ~A~n" inp result)))
      (error "test" 
	     (format "Expected ~A but got ~A on input~n~A~n" expected result inp)
	     (list inp expected)))
  #t)

(define (test-same inp)
  (test inp inp))


(define (test-all)
  ;; test that some common cpp patterns are passed through:
  (test-same "abcd")
  (test-same "#define X 1")
  (test-same "#define f(X) (X+1)\nf(10)")
  (test-same "abc##def")
  (test "abc#(+ 2 2)def" "abc4def")
  (test "abc#[define my-var 10]def#{my-var}" "abcdef10")
  (test "abc#[define foo \"abc\"]def" "abcdef")
  (test "11#{foo}22" "11abc22")
  (test "a#[define (f x) (+ 2 x)]ab#(f 4)b" "aab6b")
;;   ; this would fail with an error message:
;;   (preprocess-string "a#[y]")
  (display "ALL PASSED\n")
  ""
  )

(define (main argv)
  (test-all))

